var ngdom = {
	version: '1.0',
	build: '00237',
	Engine: null,
	Utils: {
		queryValue: function (key) {
			var qs = window.location.search.substring(1);
			var params = qs.split("&");
			
			for (var i = 0; i < params.length; i++) {
				var keyvalue = params[i].split("=");
				if (keyvalue[0] == key) {
					return keyvalue[1];
				}
			}
		},
		getNameFromFunction: function (namedFunction) {
			var name = namedFunction.toString();
			name = name.substr('function '.length);
			name = name.substr(0, name.indexOf('('));
			return name;
		},
		getObjectClass: function (obj) {
			if (obj && obj.constructor && obj.constructor.toString) {
				var arr = obj.constructor.toString().match(/function\s*(\w+)/);
				if (arr && arr.length == 2) {
					return arr[1];
				}
			}
			return undefined;
		},
		distanceSquared: function (pos1, pos2) {
			var diffX = pos2.x - pos1.x, diffY = pos2.y - pos1.y;
			return diffX * diffX + diffY * diffY;
		},
		distance: function (pos1, pos2) {
			return Math.sqrt(this.distanceSquared(pos1, pos2));
		}
	},
	enemyClasses: [],
	init: function (engine) {
		if (typeof(engine) == 'undefined') engine = new ngdomEngine();
		this.Engine = engine;
		jQuery(function($) {
			$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
				if (options.url == 'game.php')
					options.data += '&game=' + ngdomData.game;
			});
			ngdom.Engine.init();
		});
		
		this.browser = {
			iPhone: navigator.userAgent.indexOf("iPhone") != -1,
			iPod: navigator.userAgent.indexOf("iPod") != -1,
 			iPad: navigator.userAgent.indexOf("iPad") != -1,
 			Android: navigator.userAgent.match(/Android/i)
 		};
 		this.browser.iOS = this.browser.iPhone || this.browser.iPod || this.browser.iPad;
	},
	log: function (message) {
		if (ngdom.Engine.debug) console.log(message);
	}
};

if (!window.console) console = {};
console.log = console.log || function(){};
console.assert = console.assert || function(){};

Function.prototype.inherits = function (base) {
	if (base.constructor == Function) 
	{ 
		//Normal Inheritance 
		this.prototype = new base;
		this.prototype.constructor = this;
		this.prototype.base = base.prototype;

		//keep a list of defined enemy classes for builder (hack but oh well)
		if (typeof(ngdomSpriteEnemy) != 'undefined' && (base == ngdomSpriteEnemy || (typeof(base.prototype.base) != 'undefined' && base.prototype.base.constructor == ngdomSpriteEnemy)))
			ngdom.enemyClasses[ngdom.enemyClasses.length] = ngdom.Utils.getNameFromFunction(this);
	} 
	else 
	{ 
		//Pure Virtual Inheritance 
		this.prototype = base;
		this.prototype.constructor = this;
		this.prototype.base = base;
	} 
	return this;
};